package br.com.util.lov;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BuscaServlet extends HttpServlet
{

   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException
   {
      doPost(request, response);
   }

   public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException
   {
      try
      {
         String campoProxFoco = "";
         String campoDescricao = "";
         String codigo = "";
         
         if(request.getParameter("cmpFc") != null)
         {
            campoProxFoco = request.getParameter("cmpFc");
         }
         
         if(request.getParameter("cmpDs") != null)
         {
            campoDescricao = request.getParameter("cmpDs");
         }
         
         if(request.getParameter("vlrCd") != null)
         {
            codigo = request.getParameter("vlrCd");
         }
         
         if (request.getParameter("tp") != null)
         {
            String tipoLista = request.getParameter("tp");
            ParametroListaVo parListaVo = ListaNgc.getInstancia().getParametro(tipoLista);
            if (parListaVo != null)
            {
               if(parListaVo.getFiltros() != null)
               {
                  Iterator iterator = parListaVo.getFiltros().iterator();
                  while(iterator.hasNext())
                  {
                     FiltroListaVo filtroVo = (FiltroListaVo) iterator.next();
                     
                     String temp = request.getParameter(filtroVo.getNome());
                     if(temp != null)
                     {
                        filtroVo.setValor(temp);
                     }
                  }
               }
               
               String descricao = ListaNgc.getInstancia().getDescricao(parListaVo, codigo);
               descricao = "<![CDATA[" + descricao + "]]>";
               String busca = "<Busca>"
                  + "<Descricao>"
                  + descricao
                  + "</Descricao>"
                  + "</Busca>";
               response.setContentType("text/xml;charset=UTF-8");
               response.setHeader("Cache-Control", "no-cache");
               response.getWriter().write(busca);
            }
         }   
      }
      catch (Exception e) 
      {
         System.out.println("");
      }
      catch (Throwable t) 
      {
         System.out.println("");
      }
   }

}